package com.spa.application.service.member;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.member.MemberServiceDTO;
import com.spa.application.dto.member.MemberServiceDetailsCarouselDTO;
import com.spa.application.query.member.MemberServiceDetailsQuery;
import com.spa.application.query.member.MemberServiceQuery;
import com.spa.domain.entity.MassageServiceEntity;
import com.spa.domain.service.MassageService;
import com.spa.domain.service.OrderService;
import com.spa.infrastructure.mapper.MassageServiceFlowMapperStruct;
import com.spa.infrastructure.mapper.MassageServiceMapperStruct;
import com.spa.infrastructure.repository.condition.OrderInfoCondition;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class MemberServiceApplicationService {

    //服务service
    @Resource
    private MassageService massageService;
    @Resource
    private OrderService orderService;

    /**
     * 获取服务分页
     *
     * @return 结果
     */
    public PageResult<MemberServiceDTO> page(MemberServiceQuery query) {
        Page<MassageServiceEntity> pageDtoList = massageService.servicePageList(MassageServiceMapperStruct.INSTANCE.query2Condition(query));
        PageResult<MemberServiceDTO> servicePageDTO = MassageServiceMapperStruct.INSTANCE.entity2MemberDto4Page(pageDtoList);
        if (ObjectUtils.isNotEmpty(servicePageDTO) && CollUtil.isNotEmpty(servicePageDTO.getRecords())) {
            servicePageDTO.getRecords().forEach(a -> {
                //获取多少订单是指定的服务
                OrderInfoCondition orderInfoCondition = new OrderInfoCondition();
                orderInfoCondition.setServiceId(a.getId());
                a.setSelectCount(orderService.countByCondition(orderInfoCondition) + a.getSelectCount());
                a.setTag((List<String>) Convert.toList(a.getHighlightsTar()));
            });
        }
        return servicePageDTO;
    }

    /**
     * 详情
     *
     * @return 结果
     */
    public MemberServiceDTO details(MemberServiceDetailsQuery query) {
        MemberServiceQuery q = new MemberServiceQuery();
        q.setServiceId(query.getId());
        MassageServiceEntity massageServiceEntity = massageService.serviceDetails(MassageServiceMapperStruct.INSTANCE.query2Condition(q));
        Assert.isTrue(BeanUtil.isNotEmpty(massageServiceEntity), "该详情不存在");
        MemberServiceDTO dto = MassageServiceMapperStruct.INSTANCE.entity2MemberDto(massageServiceEntity);


        dto.setRecommend(massageServiceEntity.getRecommendations());

        List<String> d = (List<String>) Convert.toList(massageServiceEntity.getHighlightsTar());
        dto.setTag(d);


        dto.setTabooDesc(Lists.newArrayList(massageServiceEntity.getNote().split("\\|")));

        dto.setInstruction(Lists.newArrayList(massageServiceEntity.getDeclaration().split("\\|")));


        List<MemberServiceDetailsCarouselDTO> carouselDTOList = new ArrayList<>();

        List<String> imgList = (List<String>) Convert.toList(massageServiceEntity.getImg());
        imgList.stream().forEach(a -> {
            MemberServiceDetailsCarouselDTO carouselDTO = new MemberServiceDetailsCarouselDTO();
            carouselDTO.setUrl(a);

            carouselDTOList.add(carouselDTO);
        });

        //轮播图
        dto.setCarouselList(carouselDTOList);
        OrderInfoCondition orderInfoCondition = new OrderInfoCondition();
        orderInfoCondition.setServiceId(dto.getId());
        dto.setSelectCount(orderService.countByCondition(orderInfoCondition) + dto.getSelectCount());

        //服务流程
        dto.setMassageServiceFlowList(MassageServiceFlowMapperStruct.INSTANCE.list2MemberDto4List(massageServiceEntity.getMassageServiceFlowList()));


        return dto;
    }




}
